﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WPFCustomControlExample.ViewModel;
using ZCustomControl.Controls;
using ZCustomControl.Models;

namespace WPFCustomControlExample
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        #region Fields

        private MainWindowViewModel _viewModel = null;

        #endregion

        #region Constructors

        public MainWindow()
        {
            InitializeComponent();

            _viewModel = new MainWindowViewModel();
            DataContext = _viewModel;

            winCtrl.CloseConfirm = new Func<bool>(ClsoeWindowConfirm);
        }
        
        #endregion

        #region Event Methods

        private void header_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2)
            {
                this.WindowState = WindowState == WindowState.Normal ? WindowState.Maximized : WindowState.Normal;
                winCtrl.WindowControlType = (WindowControlType)((int)WindowState);
            }
            else
            {
                this.DragMove();
            }
        }


        private void winCtrl_ChangeWindowState(object sender, RoutedEventArgs e)
        {
            WindowController winCtrl = sender as WindowController;
            if(winCtrl == null)
            {
                return;
            }

            if(winCtrl.WindowControlType == WindowControlType.Close)
            {
                Close();
            }
            else if(winCtrl.WindowControlType != WindowControlType.None)
            {
                WindowState = (WindowState)((int)winCtrl.WindowControlType);
            }
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// 窗口关闭确认方法
        /// </summary>
        /// <returns></returns>
        private bool ClsoeWindowConfirm()
        {
            MessageBoxResult result = MessageBox.Show("确认关闭当前窗口？", "窗口关闭确认", MessageBoxButton.OKCancel);
            if(result == MessageBoxResult.OK)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        #endregion

    }
}
